package leetcodecompetition.D347;

/**
 * @Author: Siantar
 * @Date: 2023-05-28-11:22
 * @Description: 1.0
 */
public class T6455 {
    public long minimumCost(String s) {
        char[] ss = s.toCharArray();
        int n = s.length();
        long[] left = new long[n];
        long[] right = new long[n];
        long l = 0;
        for (int i = 0; i < n - 1; i++) {
            if (ss[i] != ss[i + 1]) {
                l += i + 1;
            }
            left[i + 1] = l;
        }
        long r = 0;
        for (int i = n - 1; i >= 1; i--) {
            if (ss[i] != ss[i - 1]) {
                r += n - i;
            }
            right[i - 1] = r;
        }
        long min = Long.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            min = Math.min(min, left[i] + right[i]);
        }
        return min;
    }
}
